import pandas as pd
import numpy as np
import lightgbm as lgb
from tiancheng.base.base_helper import *
from sklearn.model_selection import StratifiedKFold


# op_train = get_operation_train_new()
# trans_train = get_transaction_train_new()
# op_test = get_operation_round1_new() # pd.read_csv('input/operation_round1_new.csv')
# trans_test = get_transaction_round1_new() #pd.read_csv('input/transaction_round1_new.csv')
# op_merge = pd.read_pickle(features_base_path+"op_merge01.pkl")
# tst_merge = pd.read_pickle(features_base_path+"tst_merge.pkl")
# op_train = op_merge[op_merge[tag_hd.Tag]!=-1]
# op_test = op_merge[op_merge[tag_hd.Tag]==-1]
# trans_train = tst_merge[tst_merge[tag_hd.Tag]!=-1]
# trans_test = tst_merge[tst_merge[tag_hd.Tag]==-1]
# op_train.pop(tag_hd.Tag)
# op_test.pop(tag_hd.Tag)
# trans_train.pop(tag_hd.Tag)
# trans_test.pop(tag_hd.Tag)
y = get_tag_train_new() # pd.read_csv('input/tag_train_new.txt')
sub = get_sub()


# def get_feature(op, trans, label):
#     op_gp_uid = op.groupby(['UID'])
#     for feature in op.columns[2:]:
#         print(feature)
#         label = label.merge(op_gp_uid[feature].count().reset_index(), on='UID', how='left')
#         label = label.merge(op_gp_uid[feature].nunique().reset_index(), on='UID', how='left')
#     trans_gp_uid = trans.groupby(['UID'])
#     for feature in trans.columns[2:]:
#         print(feature)
#         if trans_train[feature].dtype == 'object':
#             label = label.merge(trans_gp_uid[feature].count().reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].nunique().reset_index(), on='UID', how='left')
#         else:
#             label = label.merge(trans_gp_uid[feature].count().reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].nunique().reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].max().reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].min().reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].sum().reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].mean().reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].std().reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].var().reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].quantile(.25).reset_index(), on='UID', how='left')
#             label = label.merge(trans_gp_uid[feature].quantile(.75).reset_index(), on='UID', how='left')
#     return label


# train = get_feature(op_train, trans_train, y).fillna(-1)
# test = get_feature(op_test, trans_test, sub).fillna(-1)
train = get_train()
test = get_test()
train = train.drop(['UID', 'Tag'], axis=1)
train = fill_mean(train)
test = fill_mean(test)
label = y['Tag']

test_id = get_sub()['UID']
test = test.drop(['UID', 'Tag'], axis=1)
print(train.columns.values)

lgb_model = lgb.LGBMClassifier(boosting_type='gbdt', num_leaves=300, reg_alpha=3, reg_lambda=5, max_depth=8,
                               n_estimators=5000, objective='binary', subsample=0.75, colsample_bytree=0.77,
                               subsample_freq=1, learning_rate=0.05,
                               random_state=1000, n_jobs=16, min_child_weight=4, min_child_samples=5, min_split_gain=0)
skf = StratifiedKFold(n_splits=5, random_state=2018, shuffle=True)
best_score = []

oof_preds = np.zeros(train.shape[0])
sub_preds = np.zeros(test_id.shape[0])
label = train['label']
train.pop('label')
# pd.to_pickle(train,features_base_path+"train.pkl")
# pd.to_pickle(test,features_base_path+"test.pkl")

for index, (train_index, test_index) in enumerate(skf.split(train, label)):
    lgb_model.fit(train.iloc[train_index], label.iloc[train_index], verbose=10,eval_metric="binary_logloss",
                  eval_set=[(train.iloc[train_index], label.iloc[train_index]),
                            (train.iloc[test_index], label.iloc[test_index])], early_stopping_rounds=30)
    best_score.append(lgb_model.best_score_['valid_1']['binary_logloss'])
    print(best_score)
    oof_preds[test_index] = lgb_model.predict_proba(train.iloc[test_index], num_iteration=lgb_model.best_iteration_)[:,1]

    test_pred = lgb_model.predict_proba(test, num_iteration=lgb_model.best_iteration_)[:, 1]
    sub_preds += test_pred / 5
    # print('test mean:', test_pred.mean())
    # predict_result['predicted_score'] = predict_result['predicted_score'] + test_pred

m = tpr_weight_funtion(y_predict=oof_preds, y_true=label)
print(m)
sub['Tag'] = sub_preds
sub.to_csv(sub_base_path + 'baseline_%s.csv' % str(m), index=False)
